﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Ribbon;
using Telmex;

namespace Confree
{
    /// <summary>
    /// Ribbon for the Conference tab.
    /// </summary>
    public partial class ConferenceRibbon
    {
        public ConferenceRibbon()
            : base(Globals.Factory.GetRibbonFactory())
        {
            InitializeComponent();

            // Set here the position of the ribbon because a known issue
            // of the Visual Studio 2010 Ribbon Designer
            this.groupConference.Position = this.Factory.RibbonPosition.BeforeOfficeId("GroupAppointmentMoreOptions");

            // Set-up the configuration entry-points
            this.buttonConfigure.Click += (s, e) => { ShowOptions(); };
            this.groupConference.DialogLauncherClick += (s, e) => { ShowOptions(); };

            // The conference magic
            this.buttonSetupConference.Click += (s, e) =>
                {
                    // This should take into account when the user doesn't complete
                    // all the required data; is not elegant, but for now is ok.
                    if (!TelmexConfigurationSection.IsConfigured())
                        new ConfigureWindow().ShowDialog();
                    if (!TelmexConfigurationSection.IsConfigured())
                        return;

                    // For now, we only handle appointments (no emails).
                    var inspector = this.Context as Inspector;
                    var meeting = inspector.CurrentItem as AppointmentItem;

                    // Set up the conference info
                    var conf = new Conference();
                    conf.StartsOn = meeting.Start;
                    // Now set the participants number to a very high one (prev. conf.Participants = meeting.Recipients.Count;)
                    conf.Participants = 20;

                    // If there is no subject, use the organizer name
                    conf.Name = !String.IsNullOrWhiteSpace(meeting.Subject) ? meeting.Subject : meeting.Recipients[1].Name;
                    conf.Duration = new TimeSpan(0, meeting.Duration, 0);

                    // Set the tolerance, before and after the schedule
                    var tolerance = new TimeSpan(0, 30, 0);
                    conf.StartsOn = conf.StartsOn.Subtract(tolerance);
                    conf.Duration = conf.Duration.Add(tolerance + tolerance);

                    // Create the conference!
                    var api = new TelmexService();
                    api.Login();
                    var createdConference = api.CreateConference(conf);

                    var roomInfo = String.Format(
                        "Starts on: {0}.\nDuration: {1}.\nRoom number: {2}.\nPin: {3}.\nFrom USA, call {4} option 9.\nFrom Argentina, call {5}.\n\nNote that you can enter the conference 30 minutes before and after the scheduled.\n",
                        createdConference.StartsOn.Add(tolerance).ToString("yyyy/MM/dd HH:mm"),
                        createdConference.Duration.Subtract(tolerance + tolerance).ToString(@"hh\:mm"),
                        createdConference.Room,
                        createdConference.Pin,
                        createdConference.RemoteCallPhone,
                        createdConference.LocalCallPhone);

                    var document = inspector.WordEditor as Document;
                    document.Range().InsertBefore(roomInfo);
                };
        }

        /// <summary>
        /// Shows the Options dialog.
        /// </summary>
        private void ShowOptions()
        {
            new ConfigureWindow().ShowDialog();
        }
    }
}
